package top.changelife.learn.springmvc.socket.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author cz
 * @date 2019/12/21 13:43
 */
public class Client {
    public static void main(String[] args) {
        String msg = "Client data";
        try {
            // 连接本机的 8080 端口
            Socket socket = new Socket("localhost", 8080);
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 发送数据
            pw.println(msg);
            pw.flush();
            // 接收数据
            String line = is.readLine();
            System.out.println("received from server: " + line);
            // 关闭资源
            pw.close();
            is.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
